package com.sibu.yx.promotion.persistence.model.sys;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.doubo.common.model.BaseModel;
import com.doubo.common.util.StringUtil;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * Created with IntelliJ IDEA.
 * SysUser: qiujingwang
 * Date: 2016/7/20
 * Description:用户
 */
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class SysUser extends BaseModel {
    /**
     * 用户id
     */
    @Getter
    @Setter
    private Long id;

    /**
     * 组织机构id
     */
    @Getter
    @Setter
    private Long organizationId;

    /**
     * 用户名
     */
    @Getter
    @Setter
    private String userName;


    @Getter
    @Setter
    private String phone;

    /**
     * 用户昵称
     */
    @Getter
    @Setter
    private String nickName;

    /**
     * 密码
     */
    @Getter
    @Setter
    private String password;

    /**
     * 盐
     */
    @Getter
    @Setter
    private String salt;

    /**
     * 是否锁住：0-否 1-是
     */
    @Getter
    @Setter
    private Boolean locked = Boolean.FALSE;

    /**
     * 是否删除：0-否 1-是
     */
    @Getter
    @Setter
    private Boolean deleteFlag = Boolean.FALSE;

    /**
     * 创建时间
     */
    @Getter
    @Setter
    private Date createDt;

    /**
     * 更新时间
     */
    @Getter
    @Setter
    private Date updateDt;

    /**
     * 角色列表
     */
    @Getter
    @Setter
    private String roleIds;

    /**
     * 拥有的角色集合
     */
    private Set<Long> roleIdsSet;

    public SysUser() {
    }

    public SysUser(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public Set<Long> getRoleIdsSet() {
        if (roleIdsSet != null) {
            return roleIdsSet;
        }
        roleIdsSet = new HashSet<>();
        if (roleIds != null) {
            String[] dbroleIds = StringUtil.trim(roleIds, ",").split(",");
            for (String dbroleId : dbroleIds) {
                if (StringUtil.isNumeric(dbroleId)) {
                    roleIdsSet.add(Long.valueOf(dbroleId));
                }
            }
        }
        return roleIdsSet;
    }

    public boolean isAdmin(){
        return isAdmin(this.id);
    }

    public static boolean isAdmin(Long id){
        return id != null && id == 1L;
    }
}
